package com.aiju.cash.ui.setting;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.app.config.ConfigManager;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.receiver.MyReceiver;
import com.aiju.common.ActivityStack;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.L;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.SimpleDialog;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-29 16:15
 * @version: V1.0
 * @Description: 添加收银员
 */
public class ClerkAddActivity extends BaseTitleActivity {

    private ImageView imageView;
    private EditText mEditText;

    private TextView vertifyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clerk_add);
        setCenterTitle("添加收银员");
        imageView = (ImageView) findViewById(R.id.iv_discount_qr);
        mEditText = (EditText) findViewById(R.id.ev_discount_phone);


        Bitmap mBitmap = CodeUtils.createImage(ConfigManager.INIT_SERVER_HOST + "/user/inviteUI?storeId=" + mUser.getShop().getStoreId(), 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.icon_launcher));
        imageView.setImageBitmap(mBitmap);

        L.d(ConfigManager.INIT_SERVER_HOST + "/user/inviteUI?storeId=" + mUser.getShop().getStoreId());

        vertifyView = (TextView) findViewById(R.id.btn_discount_add);
        vertifyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mEditText.getText().toString();
                if (!CheckUtil.checkPhone(phone)) {
                    T.ToastShort("手机号码格式错误，请重新输入");
                    return;
                }
                v.setEnabled(false);
                timer.start();
                new LoginApi().inviteUser(phone, mUser.getShop().getStoreId(), mUser.getToken(), new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            T.ToastShort("邀约短信已发送");
                        }
                    }
                });
            }
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(MyReceiver.MESSAGE_RECEIVED_ACTION);
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(myReceiver);
        super.onDestroy();
    }

    /**
     * 后台推送
     */
    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String extras = intent.getStringExtra("extras");
            String massage = intent.getStringExtra("message");
            DataJson dataJson = null;
            try {
                JSONObject jsonObject = new JSONObject(extras);
                dataJson = new DataJson(jsonObject.getString("json"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (dataJson.isSuccess()) {
                String data = dataJson.getData().toString();

                if (data.equals("invite")) {//添加店员
                    new SimpleDialog(ActivityStack.getTopActivity())
                            .setTitileText(massage)
                            .setLeftText("确定")
                            .setRightText("")
                            .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                                @Override
                                public void onCancel() {
                                }

                                @Override
                                public void onSubmit() {
                                }
                            }).setLayoutParams().show();

                }


            }
        }

    };
    /**
     * 倒计时
     */
    private final CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            vertifyView.setText((millisUntilFinished / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            vertifyView.setEnabled(true);
            vertifyView.setText("发送");
        }
    };
}
